<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: ot_payment.php 473 2014-01-30 16:00:59Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 473 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2014-01-30 16:00:59 +0000 (Thu, 30 Jan 2014) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

class ot_payment {
	var $title, $output, $db;

	function __construct() {
		global $db;
		$this->code = 'ot_payment';
		$this->title = MODULE_ORDER_TOTAL_PAYMENT_TITLE;
		$this->description = MODULE_ORDER_TOTAL_PAYMENT_DESCRIPTION;
		$this->enabled = (MODULE_ORDER_TOTAL_PAYMENT_STATUS == 'true' ? true : false);
		$this->sort_order = MODULE_ORDER_TOTAL_PAYMENT_SORT_ORDER;
		$this->include_shipping = MODULE_ORDER_TOTAL_PAYMENT_INC_SHIPPING;
		$this->include_tax = MODULE_ORDER_TOTAL_PAYMENT_INC_TAX;
		$this->percentage = MODULE_ORDER_TOTAL_PAYMENT_PERCENTAGE;
		$this->percentage2 = MODULE_ORDER_TOTAL_PAYMENT_PERCENTAGE2;
		$this->calculate_tax = MODULE_ORDER_TOTAL_PAYMENT_CALC_TAX;
		$this->howto_calc = MODULE_ORDER_TOTAL_PAYMENT_HOWTO_CALC;
		$this->amount1_desc = MODULE_ORDER_TOTAL_PAYMENT_AMOUNT1;
		$this->amount2_desc = MODULE_ORDER_TOTAL_PAYMENT_AMOUNT2;
		$this->output = array();
		$this->db = $db;
	}

  function process($values_from_cart = false, &$order_total_array) {
		global $order, $currencies, $price;

    $order_total_base = $order_total_array['all'];
    $order_total_base['code'] = $this->code;
    $order_total_base['title'] = $this->title;
    $order_total_base['sort_order'] = $this->sort_order;
		
		$allowed_zones = explode(',', MODULE_ORDER_TOTAL_PAYMENT_ALLOWED);

		if($this->enabled && 
			(in_array($_SESSION['delivery_zone'], $allowed_zones) == true || MODULE_ORDER_TOTAL_PAYMENT_ALLOWED == '')) {

			//$discount = $this->calculate_credit($this->order_total());
			$order_total_base['netto'] = $this->calculate_credit($order_total_base['netto'])['sum'];					
			$order_total_base['brutto'] = $this->calculate_credit($order_total_base['brutto'])['sum'];
			foreach($order_total_base['tax'] as &$tax) {
				$tax['value'] = $this->calculate_credit($tax['value'])['sum'];
			}
			
			if($this->calculate_tax == 'true')
				$order_total_base['value'] = $order_total_base['brutto'];
			else
				$order_total_base['value'] = $order_total_base['netto'];

			if($order_total_base['value'] != 0) {
				$order_total_base['text'] = $price->format($order_total_base['value'], true);
				$order_total_array[$this->code] = $order_total_base;
			}

			if ($discount['sum']!=0) {
				$this->deduction = $discount['sum'];
				if ($discount['amount1']!=0) {
					$this->output[] = array('title' => abs($discount['pro1'])."% ".$this->amount1_desc.':',
					                  		'text' => $discount['amount1']>0?'<span style="color:#b20000">'.$price->format($discount['sum'], true).'</span>':$price->format($discount['sum'], true),
					                  		'value' => $discount['sum']);
				
				} elseif ($discount['amount2']!=0) {
					$this->output[] = array('title' => abs($discount['pro2'])."% ".$this->amount2_desc.':',
					                  		'text' => $discount['amount2']>0?'<span style="color:#b20000">'.$price->format($discount['sum'], true).'</span>':$price->format($discount['sum'], true),
					                  		'value' => $discount['sum']);
				}
				$order->info['total'] = $order->info['total'] + $discount['sum'];
			}
		}
	}

	function calculate_credit($amount) {
		global $order, $customer_id, $payment;
		$od_amount = 0;
		$od_amount2 = 0;
		$discount = array();
		$tod_amount = 0;
		$tod_amount2 = 0;
		$do = false;

		$discount_table = (explode(':', $this->percentage));
		for ($i=0; $i<sizeof($discount_table); $i+=2) {
			if ($amount >= $discount_table[$i]) {
				$od_pc = $discount_table[$i+1];
				$minimum = $discount_table[$i];
			} else
				break;
		}

		$discount_table = (explode(':', $this->percentage2));
	
		for ($i=0; $i<sizeof($discount_table); $i+=2) {
			if ($amount >= $discount_table[$i]) {
				$od_pc2 = $discount_table[$i+1];
				$minimum2 = $discount_table[$i];
			} else
				break;
		}

		if($amount >= $minimum) {
			$table = explode(',', MODULE_ORDER_TOTAL_PAYMENT_TYPE);
			for ($i = 0; $i < count($table); $i++) {
				if($_SESSION['payment'] == $table[$i]) 
					$do = true;
			}
			if ($do) {
/*				if($this->calculate_tax == 'true') {
					$tod_amount = round($order->info['tax']*100)/100*$od_pc/100;
					$order->info['tax'] = $order->info['tax'] - $tod_amount;
					reset($order->info['tax_groups']);
					while (list($key, $value) = each($order->info['tax_groups'])) {
						$god_amount = round($value*100)/100*$od_pc/100;
						$order->info['tax_groups'][$key] = $order->info['tax_groups'][$key] - $god_amount;
					}
				}
*/
				$od_amount = round($amount*100)/100*$od_pc/100;
			}
		}
	
		$amount2 = $amount - $od_amount;
		$do = false;
		if ($amount2 >= $minimum2) {
			$table = explode(',' , MODULE_ORDER_TOTAL_PAYMENT_TYPE2);
			for ($i = 0; $i < count($table); $i++) {
				if ($_SESSION['payment'] == $table[$i]) 
					$do = true;
			}
			if ($do) {
/*				if($this->calculate_tax == 'true') {
					$tod_amount2 = round($order->info['tax']*100)/100*$od_pc2/100;
					$order->info['tax'] = $order->info['tax'] - $tod_amount2;
					reset($order->info['tax_groups']);
					while (list($key, $value) = each($order->info['tax_groups'])) {
						$god_amount2 = round($value*100)/100*$od_pc2/100;
						$order->info['tax_groups'][$key] = $order->info['tax_groups'][$key] - $god_amount2;
					}
				}
*/
				$od_amount2 = round($amount2*100)/100*$od_pc2/100;
			}
		}
		$discount['sum'] = -($od_amount + $od_amount2);
		$discount['amount1'] = $od_amount;
		$discount['amount2'] = $od_amount2;
		$discount['pro1'] = $od_pc;
		$discount['pro2'] = $od_pc2;
		return $discount;
	}

	function order_total() {
		global  $order, $cart;
		$order_total = $order->info['total'];
		$products = $_SESSION['cart']->get_products();
		for ($i=0; $i<sizeof($products); $i++) {
			$t_prid = get_prid($products[$i]['id']);
			$gv_result = $this->db->db_query("SELECT products_price, products_tax_class_id, products_model FROM ".TABLE_PRODUCTS." WHERE products_id = '".$t_prid."'");
			if (preg_match('/^GIFT/i', addslashes($gv_result->fields['products_model']))) {
				$qty = $cart->get_quantity($t_prid);
				$products_tax = get_tax_rate($gv_result->fields['products_tax_class_id']);
				if ($this->include_tax =='false')
					$gv_amount = $gv_result->fields['products_price'] * $qty;
				else
					$gv_amount = ($gv_result->fields['products_price'] + calculate_tax($gv_result->fields['products_price'],$products_tax)) * $qty;
				
				$order_total=$order_total - $gv_amount;
			}
		}
		if ($this->include_shipping == 'false') 
			$order_total=$order_total-$order->info['shipping_cost'];
			
		if ($this->include_tax == 'false') 
			$order_total=$order_total-$order->info['tax'];
			
		return $order_total;
	}

	function check() {
		if (!isset($this->check)) {
			$check = $this->db->db_query("SELECT configuration_value FROM ".TABLE_CONFIGURATION." WHERE configuration_key = 'MODULE_ORDER_TOTAL_PAYMENT_STATUS'");
			$this->_check = $check->_numOfRows;
		}
		return $this->_check;
	}

	function keys() {
		return array('MODULE_ORDER_TOTAL_PAYMENT_STATUS', 
					'MODULE_ORDER_TOTAL_PAYMENT_SORT_ORDER', 
					'MODULE_ORDER_TOTAL_PAYMENT_PERCENTAGE', 
					'MODULE_ORDER_TOTAL_PAYMENT_PERCENTAGE2', 
					'MODULE_ORDER_TOTAL_PAYMENT_TYPE', 
					'MODULE_ORDER_TOTAL_PAYMENT_TYPE2', 
					'MODULE_ORDER_TOTAL_PAYMENT_INC_SHIPPING', 
					'MODULE_ORDER_TOTAL_PAYMENT_INC_TAX', 
					'MODULE_ORDER_TOTAL_PAYMENT_CALC_TAX', 
					'MODULE_ORDER_TOTAL_PAYMENT_ALLOWED', 
					'MODULE_ORDER_TOTAL_PAYMENT_TAX_CLASS');
	}

	function install() {
		$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." (configuration_key, configuration_value, configuration_group_id, sort_order, set_function, date_added) VALUES ('MODULE_ORDER_TOTAL_PAYMENT_STATUS', 'true', '6', '1','cfg_select_option(array(\'true\', \'false\'), ', now())");
		$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." (configuration_key, configuration_value, configuration_group_id, sort_order, date_added) VALUES ('MODULE_ORDER_TOTAL_PAYMENT_SORT_ORDER', '49', '6', '2', now())");
		$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." (configuration_key, configuration_value, configuration_group_id, sort_order, set_function ,date_added) VALUES ('MODULE_ORDER_TOTAL_PAYMENT_INC_SHIPPING', 'false', '6', '5', 'cfg_select_option(array(\'true\', \'false\'), ', now())");
		$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." (configuration_key, configuration_value, configuration_group_id, sort_order, set_function ,date_added) VALUES ('MODULE_ORDER_TOTAL_PAYMENT_INC_TAX', 'true', '6', '6','cfg_select_option(array(\'true\', \'false\'), ', now())");
		$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." (configuration_key, configuration_value, configuration_group_id, sort_order, date_added) VALUES ('MODULE_ORDER_TOTAL_PAYMENT_PERCENTAGE', '100:4', '6', '4', now())");
		$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." (configuration_key, configuration_value, configuration_group_id, sort_order, date_added) VALUES ('MODULE_ORDER_TOTAL_PAYMENT_PERCENTAGE2', '100:2', '6', '4', now())");
		$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." (configuration_key, configuration_value, configuration_group_id, sort_order, set_function ,date_added) VALUES ('MODULE_ORDER_TOTAL_PAYMENT_CALC_TAX', 'true', '6', '5','cfg_select_option(array(\'true\', \'false\'), ', now())");
		$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." (configuration_key, configuration_value, configuration_group_id, sort_order, date_added) VALUES ('MODULE_ORDER_TOTAL_PAYMENT_TYPE', 'moneyorder', '6', '3', now())");
		$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." (configuration_key, configuration_value, configuration_group_id, sort_order, date_added) VALUES ('MODULE_ORDER_TOTAL_PAYMENT_TYPE2', 'cod', '6', '3', now())");
		$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." (configuration_key, configuration_value, configuration_group_id, sort_order, date_added) VALUES ('MODULE_ORDER_TOTAL_PAYMENT_ALLOWED', '',   '6', '2', now())");
		$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." (configuration_key, configuration_value, configuration_group_id, sort_order, use_function, set_function, date_added) VALUES ('MODULE_ORDER_TOTAL_PAYMENT_TAX_CLASS', '0','6', '7', 'get_tax_class_title', 'cfg_pull_down_tax_classes(', now())");
	}

	function remove() {
		$keys = '';
		$keys_array = $this->keys();
		for ($i=0; $i<sizeof($keys_array); $i++)
			$keys .= "'".$keys_array[$i]."',";

		$keys = substr($keys, 0, -1);
		$this->db->db_query("DELETE FROM ".TABLE_CONFIGURATION." WHERE configuration_key IN (".$keys.")");
	}
}
?>